package com.bicon.botu.mall.member.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.member.entity.Address;
import com.bicon.botu.mall.member.service.IAddressService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.MatcherUtil;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.mall.member.common.MemberConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;



/**
 * 收货地址客户端对接接口
 *
 * @Author 李双喜
 * @Date 2017/7/19 15:03.
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/api/Address")
public class AddressApi extends BaseController {
    @Autowired
    private IAddressService iAddressService;

    /**
     * 新增收货地址
     *
     * @param addressName          收货人
     * @param addressPhone         手机号码
     * @param addressProvince      省份
     * @param addressCity          城市
     * @param addressArea          县区
     * @param addressStreet        街道
     * @param addressZip           邮编
     * @param addressDetailed      详细地址
     * @param addressTag           标签
     * @param addressDefaultChoose 默认地址  0默认  1非默认
     * @return
     */
    @RequestMapping(value = "/addAddress", method = RequestMethod.POST)
    public BaseResult addAddress(@RequestParam(value = "userId") String userId, @RequestParam(value = "addressName") String addressName,
                                 @RequestParam(value = "addressPhone") String addressPhone, @RequestParam(value = "addressProvince") String addressProvince,
                                 @RequestParam(value = "addressCity") String addressCity, @RequestParam(value = "addressArea") String addressArea,
                                 @RequestParam(value = "addressStreet" ,required = false) String addressStreet, @RequestParam(value = "addressZip",required = false) String addressZip,
                                 @RequestParam(value = "addressDetailed") String addressDetailed,
                                 @RequestParam(value = "addressTag", required = false) String addressTag,
                                 @RequestParam(value = "addressDefaultChoose") String addressDefaultChoose) {
        Address address = new Address();
        address.setUserId(userId);
        if (addressName.length()>10){
            return new BaseResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400);
        }
        address.setAddressName(addressName);
        if (!MatcherUtil.matcherPhone(addressPhone)){
            return new BaseResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400);
        }
        address.setAddressPhone(addressPhone);
        address.setAddressProvince(addressProvince);
        address.setAddressCity(addressCity);
        address.setAddressArea(addressArea);
        address.setAddressStreet(addressStreet);
        address.setAddressZip(addressZip);
        if (addressDetailed.length()>40){
            return new BaseResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400);
        }
        address.setAddressDetailed(addressDetailed);
        address.setAddressTag(addressTag);
        address.setAddressDefaultChoose(addressDefaultChoose);
        boolean result = iAddressService.addAddress(address);
        if (result) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200,address.getId());
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
    }


    /**
     * 逻辑删除收货地址(根据主键id删除)
     *
     * @param id   收货地址id
     * @return
     */
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    public BaseResult deleteAddress(@RequestParam String id) {
      boolean result = iAddressService.deleteAddress(id);
       if (result){
           return new BaseResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200);
       }
        return new BaseResult(Constants.RESPONSE_CODE_500,Constants.RESPONSE_MESSAGE_500);
    }

    /**
     * 查询所有收货地址
     * @param userId  用户id
     * @return
     */
    @RequestMapping(value = "/getAddress", method = RequestMethod.POST)
    public BaseResult findById(@RequestParam("userId") String userId) {
        //根据用户Id和逻辑删除为0查询收货地址
        List<Address> list = iAddressService.selectList(new EntityWrapper<Address>().eq("user_id", userId).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (!list.isEmpty() && null != list) {
            return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, list);
        }
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改收货地址
     *
     * @param id              地址id
     * @param addressName     收货人
     * @param addressPhone    手机号码
     * @param addressProvince 省份
     * @param addressCity     城市
     * @param addressArea     县区
     * @param addressStreet   街道
     * @param addressZip      邮编
     * @param addressTag      标签
     * @param addressDetailed 详细地址
     * @return
     */
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    public BaseResult updateAddress(@RequestParam(value = "id") String id, @RequestParam(value = "addressName", required = false) String addressName,
                                    @RequestParam(value = "addressPhone", required = false) String addressPhone, @RequestParam(value = "addressProvince", required = false) String addressProvince,
                                    @RequestParam(value = "addressCity", required = false) String addressCity, @RequestParam(value = "addressArea", required = false) String addressArea,
                                    @RequestParam(value = "addressStreet", required = false) String addressStreet, @RequestParam(value = "addressZip", required = false) String addressZip,
                                    @RequestParam(value = "addressTag", required = false) String addressTag, @RequestParam(value = "addressDetailed", required = false) String addressDetailed) {
        Address address = new Address();
        //必传参数
        address.setId(id);
        //判断是否空
        if (StringUtils.isNotBlank(addressName)) {
            if (addressName.length()<=10){
                address.setAddressName(addressName);
            }
        }
        if (StringUtils.isNotBlank(addressPhone)) {
            if (MatcherUtil.matcherPhone(addressPhone)){
                address.setAddressPhone(addressPhone);
            }
        }
        if (StringUtils.isNotBlank(addressProvince)) {
            address.setAddressProvince(addressProvince);
        }
        if (StringUtils.isNotBlank(addressCity)) {
            address.setAddressCity(addressCity);
        }
        if (StringUtils.isNotBlank(addressArea)) {
            address.setAddressArea(addressArea);
        }
        if (StringUtils.isNotBlank(addressStreet)) {
            address.setAddressStreet(addressStreet);
        }
        if (StringUtils.isNotBlank(addressZip)) {
            address.setAddressDetailed(addressZip);
        }
        if (StringUtils.isNotBlank(addressDetailed)) {
            if (addressDetailed.length()<=40){
                address.setAddressDetailed(addressDetailed);
            }
        }
        if (StringUtils.isNotBlank(addressTag)) {
            address.setAddressTag(addressTag);
        }
        address.preUpdate();
        return iAddressService.updateAddress(address);
    }

    /**
     * 修改默认地址(根据id修改)，0 表示默认地址，1 表示非默认地址
     * @param userId  用户id
     * @param id      收货地址id
     * @return
     */
    @RequestMapping(value = "/updateAddressDefault", method = RequestMethod.POST)
    public BaseResult updateAddressDefault( @RequestParam ("userId")String userId, @RequestParam("id") String id) {
        //先查询出默认的地址（仅一条）
        Address entity = iAddressService.selectOne(new EntityWrapper<Address>().eq("user_id", userId)
                .eq("logic_delete", MemberConstants.DEL_LOGIC_NORMAL)
                .eq("address_default_choose", MemberConstants.ADDRESS_DEFAULT_NORMAL));
        List<Address> list = new ArrayList<>();
        if (null != entity) {
            entity.setAddressDefaultChoose(MemberConstants.ADDRESS_DEFAULT_NONE);
            entity.preUpdate();//更新
            list.add(entity);//entity放入list集合
        }
        //根据传进来的id修改为默认
        Address address = new Address().selectById(id);
        address.setAddressDefaultChoose(MemberConstants.ADDRESS_DEFAULT_NORMAL);
        address.preUpdate();//更新
        list.add(address);//address放入list集合
        //对上面list里面的两个对象统一修改（批量修改）
        boolean result = iAddressService.updateBatchById(list);
        if (result) {
            return sendResult(MemberConstants.RESPONSE_CODE_200, MemberConstants.RESPONSE_MESSAGE_200);
        } else {
            return sendResult(MemberConstants.RESPONSE_CODE_600, MemberConstants.RESPONSE_MESSAGE_600);
        }
    }
}
